import { createRouter, createWebHistory } from 'vue-router'
import store from "@/store/index"
import Layout from "@/view/layout/index.vue"
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// import { clearPending } from "@/request/index"


const routerHistory = createWebHistory()

export interface metaMode {
    requiredLogin: boolean,
    title: string,
    role?: Array<any>
}

export interface routerMode<T> {
    path: string,
    component: any,
    redirect?:string,
    name?: string,
    meta?: T,
    children?:Array<routerMode<metaMode>>
}

const publicRoutes = [
    {
        path: "/login",
        name: "login",
        component: () => import('@/view/login/index.vue'),
    },
    {
        path: "/",
        component: Layout,
        redirect: '/home',
        name: 'her',
        children: [
            {
                path: 'home',
                component: () => import('@/view/home/index.vue'),
                name: 'home',
                meta: { title: '首页', requiredLogin: true }
            }
        ]
    },
    {
        path: "/tips",
        component: Layout,
        redirect: '/tips/index',
        name: 'uuu',
        children: [
            {
                path: 'index',
                component: () => import('@/view/tips/index.vue'),
                name: 'tips',
                meta: { title: '知识库', requiredLogin: true }
            }
        ]
    },
    {
        path: "/tools",
        component: Layout,
        redirect: '/tools/index',
        name: 'jjjj',
        children: [
            {
                path: 'index',
                component: () => import('@/view/tools/index.vue'),
                name: 'tools',
                meta: { title: '小工具', requiredLogin: true }
            }
        ]
    },
    {
        path: "/case",
        component: Layout,
        redirect: '/case/index',
        name: 'case1',
        children: [
            {
                path: 'index',
                component: () => import('@/view/case/index.vue'),
                name: 'case',
                meta: { title: '小工具', requiredLogin: true }
            }
        ]
    },
    {
        path: "/router",
        component: Layout,
        redirect: '/router/index',
        name: 'router',
        children: [
            {
                path: 'index',
                component: () => import('@/view/router/index.vue'),
                name: 'router',
                meta: { title: '小工具', requiredLogin: true }
            }
        ]
    }
]

export const asyncRoutes = [
    {
        path: "/routerMoni",
        component: Layout,
        redirect: '/routerMoni/index',
        name:"route1AE",
        meta: { role: ['admin', 'editor'] },
        children: [
            {
                path: 'index',
                component: () => import('@/view/routerMoni/index.vue'),
                name: 'routerMoniIndex',
                meta: { title: 'route1AE', requiredLogin: true }
            }
        ]
    },
    {
        path: "/routerMoni1",
        component: Layout,
        redirect: '/routerMoni1/index',
        name:"routerMoni1",
        meta: { role: ['admin'] },
        children: [
            {
                path: 'index',
                component: () => import('@/view/routerMoni1/index.vue'),
                name: 'routerMoni1Index',
                meta: { title: 'routerMoni1Index', requiredLogin: true }
            }
        ]
    },
    {
        path: "/routerMoni2",
        component: Layout,
        redirect: '/routerMoni2/index',
        meta: { role: ['editor'] },
        name:"routerMoni2",
        children: [
            {
                path: '/index',
                component: () => import('@/view/routerMoni2/index.vue'),
                name: 'routerMoni2Index',
                meta: { title: 'routerMoni2Index', requiredLogin: true }
            }
        ]
    }
    
]

const router = createRouter({
    routes: publicRoutes,
    history: routerHistory,

})

router.beforeEach((to, from, next) => {
    NProgress.start()
    const { token } = store.state
    const { requiredLogin } = to.meta
    if (token) {
        next()
        // clearPending()
    }
    else {
        if (requiredLogin) {
            next({
                path: "/login"
            })
        } else {
            next()
            // clearPending()
        }
    }
})
router.afterEach(() => {
    NProgress.done()
})

export default router